package cn.cjxy.session01;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * session API
 */
@WebServlet("/demo01")
public class Session_Api extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //得到session
        HttpSession session = req.getSession();
        //String getId()得到会话的ID，在服务器上唯一的32位的十六进制数
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.println("会话id:"+session.getId());
        //long getCreationTime()表示会话创建的时间，返回long类型。表示1970-1-1到这个时间之间相差的毫秒数
        writer.println("创建时间:"+session.getCreationTime());
        //long getLastAccessedTime()表示会话上次访问的时间
        writer.println("上次访问时间:"+session.getLastAccessedTime());
        //boolean isNew()判断当前是否是一个新的会话，是的返回true
        writer.println("是否新的会话：:"+session.isNew());
        //ServletContext getServletContext()通过会话得到上下文对象
        writer.println("上下文对象："+session.getServletContext());
    }


}
